<?php 
/**
 * 
 * @author tangbl
 *
 */
class CompanyModel extends Model {
	
	/**
	 * 根据条件数组查询商户
	 * @param  array $map 条件数组
	 * @return array 商户数组
	 */
	public function search($id){
	
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Company/find",$map);
		
		return $json['data'];
	}
	
	/**
	 * 根据条件数组查询角色
	 * @param  array $map 条件数组
	 * @return array 角色数组
	 */
	public function searchRole($id){
	
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Role/find",$map);
	
		return $json['data'];
	}
	
	/**
	 * 根据条件数组查询用户
	 * @param  array $map 条件数组
	 * @return array 角色数组
	 */
	public function searchAdmin($id){
	
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Admin/find",$map);
	
		return $json['data'];
	}
	
	/**
	 * 根据条件数组查询门店
	 * @param  array $map 条件数组
	 * @return array 角色数组
	 */
	public function searchOutlet($id){
	
		$map  = array("id"=>$id);
		$json = sendRest("BDC/Outlet/find",$map);
	
		return $json['data'];
	}
	
	/**
	 * 保存登陆用户的Session信息
	 * @param $user 用户数组
	 */
	public function setLoginSession($user){
		
		$role   = M('AdminRole')->find($user['role']);
		
		session(C('USER_AUTH_KEY'),      $user['id']);
		session(C('USER_AUTH_NAME'),     $user['jobno']);
		session(C('USER_AUTH_ACCOUNT'),  $user['username']);
		session(C('USER_AUTH_ROLE'),     $user['role']);
		session(C('USER_AUTH_COMPANY'),  $user['company']);
		session(C('USER_AUTH_OUTLETID'), $user['outletid']);
		session(C('USER_AUTH_OUTLET'),   $user['outlet']);
		session(C('USER_AUTH_CITYID'),   $user['cityid']);
		session(C('USER_AUTH_ROLE_VIEW'),$role['role_view']);
		session(C('USER_AUTH_TIME_VIEW'),$role['time_view']);
		
		if($user['admin'] == 1) {
            session(C('ADMIN_AUTH_KEY'), true);
        }
        $desktop = trim($user['desktop']);
        if(empty($desktop)){
        	$desktop = C("DESKTOP_DEFAULT");
        }
        session(C('USER_DESKTOP'),$desktop);
	}
	
	/**
	 * 清空登陆用户的Session信息
	 */
	public function clearLoginSession(){
	
		unset($_SESSION[C('USER_AUTH_KEY')]);
		unset($_SESSION[C('USER_AUTH_NAME')]);
		unset($_SESSION[C('USER_AUTH_ACCOUNT')]);
		unset($_SESSION[C('USER_AUTH_ROLE')]);
		unset($_SESSION[C('USER_AUTH_COMPANY')]);
		unset($_SESSION[C('USER_AUTH_OUTLETID')]);
		unset($_SESSION[C('USER_AUTH_OUTLET')]);
		unset($_SESSION[C('ADMIN_AUTH_KEY')]);
		unset($_SESSION[C('USER_DESKTOP')]);
		
		session_destroy();

	}
		
	/**
	 * 保存用户登录的记录(IP和登陆时间)
	 * @param $user array 用户数组
	 */
	public function setLoginRecord($user){
	
		$ip	  =	get_client_ip();
		$data = array();
		if($ip){
			import('ORG.Net.IpLocation');
			$Ip = new IpLocation();
			$location = $Ip->getlocation($ip);
			$data['last_location'] = '';
			if($location['country'] && $location['country']!='CZ88.NET') $data['lastLocation'].=$location['country'];
			if($location['area']    && $location['area']!='CZ88.NET') $data['lastLocation'].=' '.$location['area'];
		}
		$data['id']	= $user['id'];
		$data['last_login_time'] =	time();
		$data['last_login_ip']	 =	get_client_ip();
		$data['login_times']	 =	$user['login_times']+1;
		$this->save($data);
	}
}